package com.mbl.base.modular.member.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mbl.base.core.common.controller.BaseController;
import com.mbl.base.core.common.dto.MblResult;
import com.mbl.base.core.common.exception.AuthException;
import com.mbl.base.core.common.exception.BaseException;
import com.mbl.base.core.utils.UtilHttp;
import com.mbl.base.core.utils.UtilJwtToken;
import com.mbl.base.core.utils.UtilSecurity;
import com.mbl.base.modular.base.entity.Admin;
import com.mbl.base.modular.member.entity.User;
import com.mbl.base.modular.member.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author lwj
 * @since 2020-03-05
 */
@RestController
@RequestMapping("/member/user")
public class UserController extends BaseController<User, IUserService> {

    @PostMapping(value = "/info")
    public MblResult info() {
        return MblResult.success(getService().getById(UtilSecurity.getUser().getId()));
    }

    @Override
    public void machiningList(QueryWrapper<User> queryWrapper, String condition) {
        final String username = UtilHttp.getPara("username");
        queryWrapper.like(StringUtils.isNotBlank(username), "username", username);
        super.machiningList(queryWrapper, condition);
    }

    @Override
    public void machiningAdd(User obj) {
        List<User> username = getService().query().eq("username", obj.getUsername()).list();
        if (username.size() > 0){
            throw new BaseException("账号重复！");
        }
        obj.setPassword(new BCryptPasswordEncoder().encode(obj.getPassword()));
        super.machiningAdd(obj);
    }


    @Override
    public void machiningListResult(IPage<User> page1) {
        final List<User> records = page1.getRecords();
        records.forEach(user -> {
            user.setPassword(null);
        });
        super.machiningListResult(page1);
    }
}

